﻿using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;
using DataCenter.Configuration;
using Common.Contract.Customer.Identifier;

namespace DataCenter.Provider
{
    public class ShopCellProvider : GenericProvider, IShopCellProvider
    {
        public List<ShopCell> SearchShopCell(out int totalRecord, string shopCode = null, string shopCell = null, bool? active = null, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ShopCell>();
            if (!string.IsNullOrWhiteSpace(shopCode))
            {
                querry = querry.Where(x => x.ShopId.ToUpper() == shopCode.ToUpper());
            }

            if (!string.IsNullOrWhiteSpace(shopCell))
            {
                querry = querry.Where(x => x.CellNo.ToUpper().Contains(shopCell.ToUpper()));
            }

            if (active.HasValue)
            {
                var userflag = active.Value.ToBooleanString();
                querry = querry.Where(x => x.UseFlag.ToUpper() == userflag);
            }
            querry = querry.OrderBy(x => x.ShopId);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            var result = querry.ToList();
            return result;
        }


        public List<ShopCell> SearchShopCell(out int totalRecord, string searchText, int pagesize = 0, int pageindex = 0)
        {

            var querry = repository.GetQuery<ShopCell>();
            if (!string.IsNullOrWhiteSpace(searchText))
            {
                querry = querry.Where(x => x.ShopId.ToUpper() == searchText.ToUpper() ||
                                           x.CellNo.ToUpper().Contains(searchText.ToUpper()));

            }

            querry = querry.OrderBy(x => x.ShopId);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            var result = querry.ToList();
            return result;
        }

        public ShopCell RetrieveShopCell(string shopcode, string cellno)
        {
            shopcode = (shopcode ?? string.Empty).ToUpper();
            cellno = (cellno ?? string.Empty).ToUpper();
            return repository.FindOne<ShopCell>(x => x.ShopId.ToUpper() == shopcode && x.CellNo.ToUpper() == cellno);
        }

        public bool Update(ShopCell shop)
        {
            try
            {
                var shopCode = shop.ShopId ?? string.Empty;
                var shopCell = shop.CellNo ?? string.Empty;
                var shopChecked = RetrieveShopCell(shopCode, shopCell);
                if (shopChecked == null) return false;

                if (shop.SoTransferDate.HasValue) shopChecked.SoTransferDate = shop.SoTransferDate;
                if (!shop.SoTransferFlag.IsNullOrWhiteSpace()) shopChecked.SoTransferFlag = shop.SoTransferFlag;
                if (!shop.UseFlag.IsNullOrWhiteSpace()) shopChecked.UseFlag = shop.UseFlag;
                if (!shop.ModifyUser.IsNullOrWhiteSpace()) shopChecked.ModifyUser = shop.ModifyUser;
                if (shop.CreateDate.HasValue) shopChecked.CreateDate = shop.CreateDate;
                if (!shop.CreateUser.IsNullOrWhiteSpace()) shopChecked.CreateUser = shop.CreateUser;
                if (shop.ModifyDate.HasValue) shopChecked.ModifyDate = shop.ModifyDate;

                shopChecked.ShopId = shop.ShopId;
                shopChecked.CellNo = shop.CellNo;
                repository.UnitOfWork.SaveChanges();
                return true;

            }
            catch
            {
                return false;
            }
        }

        public bool Add(ShopCell shop)
        {
            try
            {
                repository.Add<ShopCell>(shop);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string shopcode, string shopcell)
        {
            try
            {
                shopcode = (shopcode ?? string.Empty).ToUpper();
                shopcell = (shopcell ?? string.Empty).ToUpper();
                repository.Delete<ShopCell>(x => x.ShopId.ToUpper() == shopcode && x.CellNo.ToUpper() == shopcell);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

